package com.atguigu.gulimall.product.controller;

import com.atguigu.common.utils.CommonResponse;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.entity.vo.AttrGroupRelationVo;
import com.atguigu.gulimall.product.entity.vo.AttrGroupWithAttrsVo;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.AttrGroupService;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.utils.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 属性分组
 *
 * @author lax
 * @email lax0306@gmail.com
 * @date 2024-01-10 11:16:36
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService relationService;

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public CommonResponse list(@RequestParam Map<String, Object> params
            , @PathVariable("catelogId") Long catelogId) {
        PageUtils page = attrGroupService.queryPage(params, catelogId);
        return ResponseUtil.success(page);
    }

    @RequestMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId) {
        List<AttrEntity> entityList = attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data", entityList);
    }

    ///product/attrgroup/1/noattr/relation
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String, Object> params) {
        PageUtils page = attrService.getNoRelationAttr(params, attrgroupId);
        return R.ok().put("page", page);
    }

    ///product/attrgroup/(catelogId}/withattr
    @GetMapping("/{catelogId}/withattr")
    public R attrgroupWithattr(@PathVariable("catelogId") Long catelogId) {
        List<AttrGroupWithAttrsVo> vos = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data", vos);
    }

    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> vos) {
        relationService.addRelation(vos);
        return R.ok();
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public CommonResponse info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();
        //查询所有的父组件id
        Long[] catelogIds = categoryService.findCatelogPath(catelogId);
        attrGroup.setCatelogPath(catelogIds);
        return ResponseUtil.success(attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public CommonResponse save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);
        return ResponseUtil.success();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public CommonResponse update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);
        return ResponseUtil.success();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public CommonResponse delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));
        return ResponseUtil.success();
    }


    /**
     * 删除
     */
    @RequestMapping("/attr/relation/delete")
    public R relationdelete(@RequestBody AttrGroupRelationVo[] vos) {
        attrService.relationdelete(vos);
        return R.ok();
    }

}
